{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "d15306b7-4642-4bbe-87a7-9cdb9b7e9ff0",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Python version: 3.7.6 (tags/v3.7.6:43364a7ae0, Dec 19 2019, 00:42:30) [MSC v.1916 64 bit (AMD64)]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import os, time, sys\n",
    "import importlib as imp\n",
    "# import matplotlib.pyplot as plt\n",
    "# from tqdm import trange\n",
    "from time import sleep\n",
    "print(\"Python version: \" + sys.version)\n",
    "lumapi = imp.machinery.SourceFileLoader(\"lumepi\",\"C:\\\\Program Files\\\\Lumerical\\\\v202\\\\api\\\\python\\\\lumapi.py\").load_module()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9b206313-7164-40ed-9d3b-a77fcaa1175a",
   "metadata": {},
   "outputs": [],
   "source": [
    "def merge_layer(final_file, temp_layer_file, object_name):   \n",
    "    print(\"Start to copy \" + str(object_name)+\"...\")\n",
    "    temp_layer = lumapi.FDTD(filename = temp_layer_file,hide = True)\n",
    "    temp_layer.select(object_name)          # select object in temp_layer_file\n",
    "    temp_layer.copytoclipboard()            # copy object(layer) to clipboard\n",
    "\n",
    "    final_simulation = lumapi.FDTD(filename = final_file,hide = True)\n",
    "    final_simulation.pastefromclipboard()   # paste object into file\n",
    "    final_simulation.save()\n",
    "\n",
    "    temp_layer.close()\n",
    "    final_simulation.close()\n",
    "    print(\"Copy\"+ str(object_name) +\"finished\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "d6ec68f4-57f7-4bcc-ba60-9ee409796f05",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Initilizing...\n",
      "Initilizing Completed\n",
      "Start to copy\n",
      "Copy finished\n"
     ]
    }
   ],
   "source": [
    "# merge all the layer into final simulation\n",
    "for l in range(0,5):\n",
    "    # specify file and object names\n",
    "    final_file = \"./D2NN_Simulation.fsp\"\n",
    "    temp_layer_file = \"./D2NN_Simulation_layer_\"+str(l)\n",
    "    object_name = \"diffraction_layer_\"+str(l)\n",
    "    merge_layer(final_file, temp_layer_file, object_name)\n",
    "    \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b226856d-1933-4294-b8e3-c9617b83a43b",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
